<?php

header("Content-type:text/html;charset=utf8");//字符编码设置

error_reporting(0);

$campus = $_POST['campus'];
$buildingname = $_POST['buildingname'];
$room = $_POST['room'];


$conn = mysqli_connect('localhost:3306','root','124938');
mysqli_select_db($conn,'schedule');
mysqli_query($conn,'SET NAMES UTF8');

$sql = "select * from class where campus='$campus' and buildingname='$buildingname' and room='$room' ";
$res = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($res);

?>


<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title>查询教室课表</title>
        <link rel="stylesheet" href="style.css" />
        <style>
            body {
                background-image: url(images/background1.png);
                background-repeat: no-repeat;
                background-position: center top;
                background-size: cover;
            }
        </style>
    </head>


    <body>
        <div class="header w">
            <div class="logo">
                <img src="images/logo.png" alt="" />
            </div>

            <div class="nav">
                <ul>
                    <li><a href="index.html" target="_self">学生课表</a></li>
                    <li><a href="classroom.html" target="_self">教室课表查询</a></li>
                    <li><a href="teacher.html" target="_self">教师课表查询</a></li>
                    <li><a href="stunotes.html" target="_self">学生笔记</a></li>
                    <li><a href="notice.html" target="_self">通知公告</a></li>
                </ul>
            </div>
        </div>
    <?php

        if (mysqli_num_rows($res) < 1){
            echo '查询无数据！';
        }
        else{
    ?>
        <div class="header w">
            <?php

            echo "<table border=6 frame = void align = center width = 1200px height = 100px cellspacing = 0px cellpadding = 10 px>";
            echo "  <tr height = 40px align = center>
                        <td ></td><td colspan = 10 color = red><font color = \"red\"><b>课表查询系统</b></font></td>
                    </tr>";
            echo "  <tr height = 50px align = center>
                        <th width = 110px></th><th><font color = blue>周日</font></th><th><font color = blue>周一</font></th><th><font color = blue>周二</font></th><th><font color = blue>周三</font></th><th><font color = blue>周四</font></th><th><font color = blue>周五</font></th><th><font color = blue>周六</font></th>
                    </tr>";
            echo "  <tr height = 40px align = center>
                        <td ></td><td colspan = 7 color = red><font color = \"red\"><b>上午</b></font></td>
                    </tr>";
            for($i=1;$i<=2;$i++) {
                echo "<tr height = 100px align = center>";
                for ($j = 0; $j < 8; $j++) {
                    if ($j == 0) {
                        if($i==1)
                            echo "<td ><font color = blue><b>第一大节</b></font></td>";
                        else
                            echo "<td ><font color = blue><b>第二大节</b></font></td>";
                    }
                    else {
                        if ($j == 1) {
                            $sql = "select * from class where campus='$campus' and buildingname='$buildingname' and room='$room' and day ='7'";
                            $res = mysqli_query($conn, $sql);
                        }
                        else {
                            $sql = "select * from class where campus='$campus' and buildingname='$buildingname' and room='$room' and day= $j-1 ";
                            $res = mysqli_query($conn, $sql);
                        }
                    }
                    if($j!=0 && $i==1){
                        $tag=0;
                        while($row=mysqli_fetch_array($res)){
                                $string = $row[6];
                                $f = explode("周",$string);
                                $ff = explode("-",$f[0]);
                                if($row[8]>='1' && $row[8]<='2'){
                                    $tag=1;
                                    $a=$row[8]+$row[9]-1;
                                    echo "<td>$row[2]<br>$row[1]<br>$row[3]<br>$row[6]<br>$row[8]-$a 节<br>$row[10]<br>$row[11]<br>$row[12]</td>";
                                    break;
                                }
                        }
                        if($tag==0) echo "<td></td>";
                    }
                    if($j!=0 && $i==2 ){
                        $tag=0;
                        while($row=mysqli_fetch_array($res)){
                            $string = $row[6];
                            $f = explode("周",$string);
                            $ff = explode("-",$f[0]);
                            if($row[8]>='3' && $row[8]<='4' ){
                                $tag=1;
                                $a=$row[8]+$row[9]-1;
                                echo "<td>$row[2]<br>$row[1]<br>$row[3]<br>$row[6]<br>$row[8]-$a 节<br>$row[10]<br>$row[11]<br>$row[12]</td>";
                                break;}
                        }
                        if($tag==0) echo "<td></td>";
                    }
                }
                echo "</tr>";
                if ($i == 1) {
                    echo "  <tr height = 40px align = center>
                                <td ></td><td colspan = 7 color = red><b>课间</b></font></td>
                            </tr>";
                }
            }


            echo "  <tr height = 40px align = center>
                        <td ></td><td colspan = 7 color = red><font color = \"red\"><b>下午</b></font></td>
                    </tr>";
            for($i=1;$i<=2;$i++) {
                echo "<tr height = 100px align = center>";
                for ($j = 0; $j < 8; $j++) {
                    if ($j == 0) {
                        if($i==1)
                            echo "<td ><font color = blue><b>第三大节</b></font></td>";
                        else
                            echo "<td ><font color = blue><b>第四大节</b></font></td>";
                    }
                    else {
                        if ($j == 1) {
                            $sql = "select * from class where campus='$campus' and buildingname='$buildingname' and room='$room' and day='7'";
                            $res = mysqli_query($conn, $sql);
                        }
                        else {
                            $sql = "select * from class where campus='$campus' and buildingname='$buildingname' and room='$room' and day= $j-1 ";
                            $res = mysqli_query($conn, $sql);
                        }
                    }
                    if($j!=0 && $i==1){
                        $tag=0;
                        while($row=mysqli_fetch_array($res)){
                            $string = $row[6];
                            $f = explode("周",$string);
                            $ff = explode("-",$f[0]);
                            if($row[8]>='5' && $row[8]<='7'){
                                $tag=1;
                                $a=$row[8]+$row[9]-1;
                                echo "<td>$row[2]<br>$row[1]<br>$row[3]<br>$row[6]<br>$row[8]-$a 节<br>$row[10]<br>$row[11]<br>$row[12]</td>";
                                break;
                            }
                        }
                        if($tag==0) echo "<td></td>";
                    }
                    if($j!=0 && $i==2){
                        $tag=0;
                        while($row=mysqli_fetch_array($res)){
                            $string = $row[6];
                            $f = explode("周",$string);
                            $ff = explode("-",$f[0]);
                            if($row[8]>='8' && $row[8]<='9'){
                                $tag=1;
                                $a=$row[8]+$row[9]-1;
                                echo "<td>$row[2]<br>$row[1]<br>$row[3]<br>$row[6]<br>$row[8]-$a 节<br>$row[10]<br>$row[11]<br>$row[12]</td>";
                                break;}
                        }
                        if($tag==0) echo "<td></td>";
                    }
                }
                echo "</tr>";
                if ($i == 1) {
                    echo "  <tr height = 40px align = center>
                                <td ></td><td colspan = 7 color = red><b>课间</b></font></td>
                            </tr>";
                }
            }


            echo "  <tr height = 40px align = center>
                        <td ></td><td colspan = 7 color = red><font color = \"red\"><b>晚上</b></font></td>
                    </tr>";

            echo "<tr height = 100px align = center>";
            for ($j = 0; $j < 8; $j++) {
                if ($j == 0) echo "<td ><font color = blue><b>第五大节</b></font></td>";
                else if ($j == 1) {
                    $sql = "select * from class where campus='$campus' and buildingname='$buildingname' and room='$room' and day='7'";
                    $res = mysqli_query($conn, $sql);
                }
                else {
                    $sql = "select * from class where campus='$campus' and buildingname='$buildingname' and room='$room' and day= $j-1 ";
                    $res = mysqli_query($conn, $sql);
                }
                if($j!=0){
                    $tag=0;
                    while($row=mysqli_fetch_array($res)){
                        $string = $row[6];
                        $f = explode("周",$string);
                        $ff = explode("-",$f[0]);
                        if($row[8]>='10' && $row[8]<='12'){
                            $tag=1;
                            $a=$row[8]+$row[9]-1;
                            echo "<td>$row[2]<br>$row[1]<br>$row[3]<br>$row[6]<br>$row[8]-$a 节<br>$row[10]<br>$row[11]<br>$row[12]</td>";
                            break;
                        }
                    }
                    if($tag==0) echo "<td></td>";
                }
            }
            echo "</tr>";

            ?>

            </table>
        </div>


        <?php
        }
        echo "<script>alert('课表显示成功！！')</script>";
        ?>
    </body>
</html>
